<?php
namespace data\set\ad;
use \data\BaseData;
/**
 * 广告
 */
class AdpositionData extends BaseData{
    
    
    /**
     * 读取一个管理员信息
     */
    public function find($param){
        $check = ['id','plat','tag'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $info  = M('set_advert_position')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一个 广告位
     */
    public function add($data){        
        $check = [
            'plat'      => ['check'=>'must','msg'=> '请选择广告位所属平台','reg'=>[1,2,3,4,5,6]],
            'channel'   => ['check'=>'must','msg'=> '请选择广告位应用渠道','reg'=>[1,2,3,4,5,6]],       
            'status'    => ['val'=>1],
            'name'      => ['check'=>'must','msg'=>'请填写广告位名称' ],
            'tag'       => ['check'=>'must','msg'=>'请填写广告位唯一标识'],
            'des'       => '',
            'width'     => ['check'=>'must','msg'=>'请填写广告位图片宽度','reg'=>'int' ],
            'height'    => ['check'=>'must','msg'=>'请填写广告位图片高度','reg'=>'int' ],
            'showpage'  => '',
            'limitnum'  => ['reg'=>'int'],
        ];        
        $save = $this->verifyData($check,$data);
        $id = M('set_advert_position')->insertGetId($save);
        if( !$id )  dataE('添加广告位失败');
        return $id;
    }
    
    /**
     * 编辑一个 广告位
     */
    public function edit($id,$data){        
        if( !$id ) dataE('修改参数不能为空');
        $check = [
            'plat'      => ['reg'=>[1,2,3,4,5,6]],
            'channel'   => ['reg'=>[1,2,3,4,5,6]],       
            'status'    => ['reg'=>[1,2]],
            'name'      => ['reg'=>'must','msg'=>'请填写广告位名称' ],
            'tag'       => ['reg'=>'must','msg'=>'请填写广告位唯一标识'],
            'des'       => '',
            'width'     => ['msg'=>'请填写广告位图片宽度','reg'=>'int' ],
            'height'    => ['msg'=>'请填写广告位图片高度','reg'=>'int' ],
            'showpage'  => '',
            'limitnum'  => ['reg'=>'int'],
        ];        
        $save = $this->verifyData($check,$data);
        if( !$save ) return false;
        $where = ['id'=>$id];
        $result = M('set_advert_position')->where($where)->update($save);
        if($result === false ) dataE('修改广告位信息失败');
        return $result;
    }
    
    /**
     * 列表读取
     */
    public function getList($param){
        $page = $param['page']?:1;
        $limit= $param['limit']?:20;
        $check = ['plat','channel','tag','status'];
        $where = $this->parseWhere($check,$param);
        if( $param['keyword'] ) $where['tag|name'] = ['like', '%'.$param['keyword'].'%'];
        $list  = M('set_advert_position')->where($where)->order('status asc,id desc')->limit($limit)->page($page)->select();
        $total = M('set_advert_position')->where($where)->count();
        return [            
            'page' => $page,
            'limit' => $limit,
            'total'=> $total,
            'list' => $list,
        ];
    }
}